package cn.iocoder.yudao.module.member.service.invitationcode;

import cn.iocoder.yudao.module.member.controller.admin.invitationcode.vo.InvitationCodePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.invitationcode.vo.InvitationCodeSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.invitationcode.InvitationCodeDO;
import cn.iocoder.yudao.module.member.dal.mysql.invitationcode.InvitationCodeMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.security.SecureRandom;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.ACTIVATION_NOT_EXISTS;

/**
 * 邀请码 Service 实现类
 *
 * @author Alan
 */
@Service
@Validated
public class InvitationCodeServiceImpl implements InvitationCodeService {
    // 定义可用字符集：0-9, A-Z, a-z，排除 I, i, L, l
    private static final String CHARACTERS = "0123456789"
            + "ABCDEFGHJKMNPQRSTUVWXYZ"  // 去掉 I 和 L
            + "abcdefghijkmnopqrstuvwxyz"; // 去掉 i 和 l

    private static final int CODE_LENGTH = 6;

    private static final SecureRandom random = new SecureRandom();

    @Resource
    private InvitationCodeMapper invitationCodeMapper;

    @Override
    public void createInvitationCode(InvitationCodeSaveReqVO createReqVO) {
        String number = createReqVO.getNumber();
        List<InvitationCodeDO> codeDOS =new ArrayList<>();
        for (int i = 0; i < Integer.parseInt(number); i++) {
            // 插入
            InvitationCodeDO invitationCode = BeanUtils.toBean(createReqVO, InvitationCodeDO.class);
            String code = generateCardCode();
            invitationCode.setNumber(code);
            invitationCode.setStatus("1");
            invitationCode.setRemainingNumber(createReqVO.getNumberDetections());
            codeDOS.add(invitationCode);
        }
        invitationCodeMapper.insertBatch(codeDOS);
    }
    public static String generateCardCode() {
        StringBuilder sb = new StringBuilder(CODE_LENGTH);
        for (int i = 0; i < CODE_LENGTH; i++) {
            int index = random.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(index));
        }
        return sb.toString();
    }
    // 测试方法
    public static void main(String[] args) {
        System.out.println("生成的检测卡号示例：");
        for (int i = 0; i < 10; i++) {
            System.out.println(generateCardCode());
        }
    }

    @Override
    public void updateInvitationCode(InvitationCodeSaveReqVO updateReqVO) {
        // 校验存在
        validateInvitationCodeExists(updateReqVO.getId());
        // 更新
        InvitationCodeDO updateObj = BeanUtils.toBean(updateReqVO, InvitationCodeDO.class);
        invitationCodeMapper.updateById(updateObj);
    }

    @Override
    public void deleteInvitationCode(Long id) {
        // 校验存在
        validateInvitationCodeExists(id);
        // 删除
        invitationCodeMapper.deleteById(id);
    }

    private void validateInvitationCodeExists(Long id) {
        if (invitationCodeMapper.selectById(id) == null) {
            throw exception(ACTIVATION_NOT_EXISTS);
        }
    }

    @Override
    public InvitationCodeDO getInvitationCode(Long id) {
        return invitationCodeMapper.selectById(id);
    }

    @Override
    public PageResult<InvitationCodeDO> getInvitationCodePage(InvitationCodePageReqVO pageReqVO) {
        return invitationCodeMapper.selectPage(pageReqVO);
    }

}